﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;
using System.IO;

namespace WineStore.Controllers
{

    public class SupplierController : Controller
    {
        // GET: /Supplier/ShowSupplier/
        public ViewResult ShowSupplier(int? page)
        {

            using (var db = new WineDbContext())
            {
                var suppliers = db.Suppliers
                    .OrderByDescending(c => c.SupplierId);

                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(suppliers.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /Supplier/CreateSupplier/
        [HttpGet]
        public ActionResult CreateSupplier()
        {
            return View();
        }

        // POST: /Supplier/CreateSupplier/
        [HttpPost]
        public ActionResult CreateSupplier(Supplier supplier, HttpPostedFileBase SupplierImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Luu anh len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Supplier"), System.IO.Path.GetFileName(SupplierImage.FileName));
                    SupplierImage.SaveAs(path);
                    // Luu anh vao database 
                    supplier.SupplierImage = SupplierImage.FileName;
                    using (var db = new WineDbContext())
                    {
                        db.Entry(supplier).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowSupplier", "Supplier");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(supplier);
        }

        // GET: /Supplier/EditSupplier/
        [HttpGet]
        public ActionResult EditSupplier(int id = 0)
        {
            Supplier supplier = null;
            using (var db = new WineDbContext())
            {
                supplier = db.Suppliers.Find(id);
                if (supplier == null)
                {
                    return HttpNotFound();
                }
            }
            return View(supplier);
        }

        // POST: /Supplier/EditSupplier/
        [HttpPost]
        public ActionResult EditSupplier(Supplier supplier, HttpPostedFileBase SupplierImage)
        {
            try
            {
                // Lay ve duong dan anh cu
                supplier.SupplierImage = (new WineDbContext().Suppliers.Find(supplier.SupplierId)).SupplierImage;
                if (ModelState.IsValidField("SupplierName") && ModelState.IsValidField("DisplayOrder"))
                {
                    // Neu update anh moi thi thuc hien khoi lenh trong than cau lenh if
                    if (SupplierImage != null)
                    {
                        // Xoa anh cu tren server
                        FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Supplier/" + supplier.SupplierImage));
                        if (fileInfo != null)
                        {
                            fileInfo.Delete();
                        }
                        // Luu anh moi len server
                        string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Supplier"), System.IO.Path.GetFileName(SupplierImage.FileName));
                        SupplierImage.SaveAs(path);
                        // Luu duong dan anh vao database 
                        supplier.SupplierImage = SupplierImage.FileName;
                    }

                    using (var db = new WineDbContext())
                    {
                        db.Entry(supplier).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("ShowSupplier", "Supplier");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(supplier);
        }


        // GET: /Supplier/DeleteSupplier/
        public ActionResult DeleteSupplier(int id)
        {
            Supplier supplier = null;
            using (var db = new WineDbContext())
            {
                supplier = db.Suppliers.Find(id);
                if (supplier == null)
                {
                    return HttpNotFound();
                }
            }
            return View(supplier);
        }

        // POST: /Supplier/DeleteSupplier/
        [HttpPost, ActionName("DeleteSupplier")]
        public ActionResult DeleteConfirmed(int id)
        {
            Supplier supplier = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    supplier = db.Suppliers.Find(id);
                    // Xoa anh tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Supplier/" + supplier.SupplierImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    db.Entry(supplier).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowSupplier", "Supplier");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(supplier);
        }

    }
}
